package org.jivesoftware.smackx.pubsub;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.UnknownFormatConversionException;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.FormField;
import org.jivesoftware.smackx.packet.DataForm;

public class SubscribeForm extends Form
{
  private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

  public SubscribeForm(Form paramForm)
  {
    super(paramForm.getDataFormToSend());
  }

  public SubscribeForm(DataForm paramDataForm)
  {
    super(paramDataForm);
  }

  public SubscribeForm(FormType paramFormType)
  {
    super(paramFormType.toString());
  }

  private void addField(SubscribeOptionFields paramSubscribeOptionFields, String paramString)
  {
    String str = paramSubscribeOptionFields.getFieldName();
    if (getField(str) == null)
    {
      FormField localFormField = new FormField(str);
      localFormField.setType(paramString);
      addField(localFormField);
    }
  }

  private String getFieldValue(SubscribeOptionFields paramSubscribeOptionFields)
  {
    return ((String)getField(paramSubscribeOptionFields.getFieldName()).getValues().next());
  }

  private Iterator<String> getFieldValues(SubscribeOptionFields paramSubscribeOptionFields)
  {
    return getField(paramSubscribeOptionFields.getFieldName()).getValues();
  }

  private static boolean parseBoolean(String paramString)
  {
    int i;
    if ((!("1".equals(paramString))) && (!("true".equals(paramString))))
      i = 0;
    while (true)
    {
      return i;
      i = 1;
    }
  }

  public int getDigestFrequency()
  {
    return Integer.parseInt(getFieldValue(SubscribeOptionFields.digest_frequency));
  }

  public Date getExpiry()
  {
    String str = getFieldValue(SubscribeOptionFields.expire);
    try
    {
      Date localDate = format.parse(str);
      return localDate;
    }
    catch (ParseException localParseException)
    {
      UnknownFormatConversionException localUnknownFormatConversionException = new UnknownFormatConversionException(str);
      localUnknownFormatConversionException.initCause(localParseException);
      throw localUnknownFormatConversionException;
    }
  }

  public Iterator<PresenceState> getShowValues()
  {
    ArrayList localArrayList = new ArrayList(5);
    Iterator localIterator = getFieldValues(SubscribeOptionFields.show_values);
    while (true)
    {
      if (!(localIterator.hasNext()))
        return localArrayList.iterator();
      localArrayList.add(PresenceState.valueOf((String)localIterator.next()));
    }
  }

  public boolean isDeliverOn()
  {
    return parseBoolean(getFieldValue(SubscribeOptionFields.deliver));
  }

  public boolean isDigestOn()
  {
    return parseBoolean(getFieldValue(SubscribeOptionFields.digest));
  }

  public boolean isIncludeBody()
  {
    return parseBoolean(getFieldValue(SubscribeOptionFields.include_body));
  }

  public void setDeliverOn(boolean paramBoolean)
  {
    addField(SubscribeOptionFields.deliver, "boolean");
    setAnswer(SubscribeOptionFields.deliver.getFieldName(), paramBoolean);
  }

  public void setDigestFrequency(int paramInt)
  {
    addField(SubscribeOptionFields.digest_frequency, "text-single");
    setAnswer(SubscribeOptionFields.digest_frequency.getFieldName(), paramInt);
  }

  public void setDigestOn(boolean paramBoolean)
  {
    addField(SubscribeOptionFields.deliver, "boolean");
    setAnswer(SubscribeOptionFields.deliver.getFieldName(), paramBoolean);
  }

  public void setExpiry(Date paramDate)
  {
    addField(SubscribeOptionFields.expire, "text-single");
    setAnswer(SubscribeOptionFields.expire.getFieldName(), format.format(paramDate));
  }

  public void setIncludeBody(boolean paramBoolean)
  {
    addField(SubscribeOptionFields.include_body, "boolean");
    setAnswer(SubscribeOptionFields.include_body.getFieldName(), paramBoolean);
  }

  public void setShowValues(Collection<PresenceState> paramCollection)
  {
    ArrayList localArrayList = new ArrayList(paramCollection.size());
    Iterator localIterator = paramCollection.iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
      {
        addField(SubscribeOptionFields.show_values, "list-multi");
        setAnswer(SubscribeOptionFields.show_values.getFieldName(), localArrayList);
        return;
      }
      localArrayList.add(((PresenceState)localIterator.next()).toString());
    }
  }
}